package com.ruoyi.web.controller.user;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUserUtils;
import com.ruoyi.user.domain.UserCert;
import com.ruoyi.user.service.UserCertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 实名认证记录Controller
 *
 * @author ruoyi
 * @date 2024-01-10
 */
@Api(tags = "实名认证")
@RestController
@RequestMapping("/user/cert")
public class UserCertController extends BaseController {
    @Autowired
    private UserCertService userCertService;

    /**
     * 查询实名认证记录列表
     */
    @PostMapping("/list")
    public TableDataInfo list(UserCert userCert) {
        startPage();
        List<UserCert> list = userCertService.selectUserCertList(userCert);
        return getDataTable(list);
    }

    /**
     * 获取实名认证记录详细信息
     */
    @PostMapping(value = "/{certId}")
    public AjaxResult getInfo(@PathVariable("certId") Long certId) {
        UserCert userCert = userCertService.selectUserCertByCertId(certId);
        if (!userCert.getUserId().equals(SecurityUserUtils.getUserId())) {
            return error("您没有权限查看");
        }
        return success(userCert);
    }

    /**
     * 保存实名认证记录
     */
    @ApiOperation("保存实名认证")
    @Log(title = "保存实名认证", businessType = BusinessType.INSERT)
    @PostMapping("save")
    public AjaxResult save(@RequestBody UserCert userCert) {
        if (userCert.getCertId() == null) {
            // 新增
            userCert.setUserId(SecurityUserUtils.getUserId());
            return toAjax(userCertService.insertUserCert(userCert));
        } else {
            // 修改，只能修改自己的
            UserCert cert = userCertService.selectUserCertByCertId(userCert.getCertId());
            if (cert == null) {
                return error("实名认证记录不存在");
            }
            if (!cert.getUserId().equals(SecurityUserUtils.getUserId())) {
                return error("您没有权限修改");
            }
            BeanUtils.copyProperties(userCert, cert, "certId", "createTime");
            return toAjax(userCertService.updateUserCert(cert));
        }
    }
}
